using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace DJLang
{
    public class DJEnv
    {
        public DJEnv parent;
        public readonly Dictionary<string, IDJValue> variables = new();

        public DJEnv()
        {

        }

        public DJEnv(DJEnv copy)
        {
            parent = copy.parent;
            variables = new Dictionary<string, IDJValue>(copy.variables);
        }
        
        public void LoadStandard(DJStdLoadFlags flags)
        {
            if (flags.HasFlag(DJStdLoadFlags.Arithmetics))
            {
                PutLibrary(typeof(DJStd.Arithmetics));
            }

            if (flags.HasFlag(DJStdLoadFlags.LanguageBasics))
            {
                PutLibrary(typeof(DJStd.LanguageBasics));
            }

            if (flags.HasFlag(DJStdLoadFlags.Debug))
            {
                PutLibrary(typeof(DJStd.Debug));
            }
        }

        public IDJValue Get(string symbol)
        {
            if (variables.ContainsKey(symbol)) return variables[symbol].Clone();
            else if (parent != null) return parent.Get(symbol);
            else return new DJErr("Undefined symbol " + symbol);
        }

        public IDJValue Get(DJSymbol symbol) => Get(symbol.name);

        public void Put(string symbol, IDJValue value)
        {
            if (variables.ContainsKey(symbol))
            {
                variables[symbol] = value;
            }
            else
            {
                variables.Add(symbol, value);
            }
        }

        public void Put(DJSymbol symbol, IDJValue value) => Put(symbol.name, value);

        public void PutFunction(string symbol, DJCallableDelegate func) => Put(symbol, new DJBuiltin(func));

        public void PutMacro(string symbol, DJCallableDelegate macro) => Put(symbol + '!', new DJBuiltinMacro(macro));

        public void PutLibrary(Type libType)
        {
            var funcs = libType.GetMethods()
                .Select(x => (info: x, attr: x.GetCustomAttribute<DJFuncAttribute>()))
                .Where(x => x.info.IsStatic && x.attr != null)
                .Select(x => ((DJCallableDelegate)x.info.CreateDelegate(typeof(DJCallableDelegate)), x.attr.FunctionName));

            foreach (var (func, name) in funcs) PutFunction(name, func);

            var macros = libType.GetMethods()
                .Select(x => (info: x, attr: x.GetCustomAttribute<DJMacroAttribute>()))
                .Where(x => x.info.IsStatic && x.attr != null)
                .Select(x => ((DJCallableDelegate)x.info.CreateDelegate(typeof(DJCallableDelegate)), x.attr.MacroName));

            foreach (var (macro, name) in macros) PutMacro(name, macro);

            var syntax = libType.GetMethods()
                .Select(x => (info: x, attr: x.GetCustomAttribute<DJSyntaxAttribute>()))
                .Where(x => x.info.IsStatic && x.attr != null)
                .Select(x => ((DJCallableDelegate)x.info.CreateDelegate(typeof(DJCallableDelegate)), x.attr.MacroName));

            foreach (var (m, name) in syntax) Put(name, new DJBuiltinMacro(m));
        }

        public void PutLibrary<T>()
        {
            PutLibrary(typeof(T));
        }

        public void PutValue<T>(string symbol, T value) where T : struct
        {
            Put(symbol, new DJBasic<T>(value));
        }

        public void PutRef<T>(string symbol, T value) where T : class
        {
            Put(symbol, new DJRef<T>(value));
        }

        public void EvalAssign(string symbol)
        {
            Put(symbol, Get(symbol).Evaluate(this));
        }

        public void EvalAssignAll()
        {
            foreach (var symbol in variables.Keys.ToArray())
            {
                EvalAssign(symbol);
            }
        }

        public IDJValue ForceEval(IDJValue value)
        {
            var evaled = value.Evaluate(this);
            if (evaled is DJList list)
            {
                return list.ToExpr().Evaluate(this);
            }
            else if (evaled is DJQuote quote)
            {
                return quote.ToSymbol().Evaluate(this);
            }
            return evaled;
        }
    }
}
